//
//  BaseController.m
//  SleepingElf
//
//  Created by karong on 2023/6/5.
//

#import "BaseController.h"
#import "BaseNavgationBar.h"
#import <AVFoundation/AVFoundation.h>

@interface BaseController ()

@property(nonatomic, strong) UIButton * leftBtn;
@property (weak, nonatomic) BaseNavgationBar *baseNavBarView;

@end

@implementation BaseController

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

//- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
//    [super traitCollectionDidChange:previousTraitCollection];
//    self.view.backgroundColor = [UIColor colorFromHexRGB:@"293774"];
//    [self initNavBarView];
//}

#ifdef DEBUG_ON

- (void)dealloc {
    WLog(@"%@:dealloc",[NSString stringWithUTF8String:object_getClassName(self)]);
}

#endif

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorFromHexRGB:@"293774"];
    [self initNavBarView];
    UIButton *btn = [self.view viewWithTag:1002];
    if(btn) {
        [btn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}

/*****************   控制台   *****************/
#pragma mark -- 声明App接收远程控制事件
-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

#pragma mark -- App结束声明接收远程控制事件
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

#pragma mark -- 接受控制台的控制事件
- (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        //判断点击按钮的类型
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                [[PlayerManager shared] resume];  //播放
                break;
            case UIEventSubtypeRemoteControlPause:
                [[PlayerManager shared] pause]; //暂停
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                [[PlayerManager shared]previous];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                [[PlayerManager shared]next];
                break;
            default:
                break;
        }
    }
}
// 初始化导航栏
- (void)initNavBarView {
    if(self.baseNavBarView) {
        if (![self.baseNavBarView isKindOfClass:BaseNavgationBar.class]) {
            UILabel *label = [self.view viewWithTag:1003];
            label.textColor = UIColor.whiteColor;
            label.font = [UIFont systemFontOfSize:18 weight:(UIFontWeightMedium)];
        }
    }
}

// 设置标題
- (void)setTitle:(NSString *)title {
    [super setTitle:title];
    UILabel *label = [self.view viewWithTag:1003];
    label.text = title;
}

- (void)backAction:(id)sender {
    if(self.navigationController) {
        if (self.navigationController.viewControllers.firstObject == self) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)popVC {
//    if (self.backBlock) {
//        self.backBlock();
//    }
//    else {
        [self.navigationController popViewControllerAnimated:YES];
//    }
}

- (UIView *)baseNavBarView {
    if (!_baseNavBarView) {
        _baseNavBarView = [self.view viewWithTag:1001];
    }
    return _baseNavBarView;
}

#pragma mark - NavigationVCDelegate

- (UIBarButtonItem *)leftBarButtonWhenEmptyForNav:(UINavigationController *)navVC {
    UIImage *image = [[UIImage imageNamed:@"ic_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return [[UIBarButtonItem alloc] initWithImage:image
                                            style:UIBarButtonItemStylePlain
                                           target:self
                                           action:@selector(popVC)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
